import {
  PipeTransform,
  Injectable,
  ArgumentMetadata,
  HttpStatus,
  HttpException,
} from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class idIsExistPipe implements PipeTransform {
  constructor(private readonly tableName: string) {}
  async transform(value: any, metadata: ArgumentMetadata) {
    const prisma = new PrismaClient();
    try {
      await prisma.postOffice.findFirst({
        where: {
          id: +value,
        },
      });
    } catch (e) {
      throw new HttpException(
        {
          code: HttpStatus.BAD_REQUEST,
          messages: 'id 不存在',
          error: 'bad request',
        },
        HttpStatus.BAD_REQUEST,
      );
    }
    return +value;
  }
}
